How to define use ansible variables?
Variables in Ansible
Ansible is a powerful automation tool that can be used to
manage infrastructure, deploy applications, and orchestrate complex workflows.
One of the key features of Ansible is its ability to use variables to define
and configure tasks and playbooks. Variables allows to reuse values across
different parts of playbook, make code more dynamic, flexible and also allow to
create flexible playbooks that can be used across different environments.
In this article, we will explore the different ways that
variables can be used in Ansible and provide some example code to illustrate
their usage.
Defining Variables
Variables can be defined in several ways in Ansible. The
most common method is to use YAML syntax to define variables directly in your
playbook. For example:
---------------------------------------------
- name: Deploy web server
hosts: web_servers
vars:
web_port: 80
app_version: 1.0.0
tasks:
- name: Install nginx
apt:
name: nginx
state: present
become: yes
- name: Configure nginx
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
mode: 0644
become: yes
In this example, we define two variables, web_port
and app_version, which can be used later in the playbook. The vars
section is used to define these variables at the playbook level, but you can
also define variables at the task or role level.
Variables are used in the tasks using Jinja2 templating
syntax. For example, if we want to use the web_port variable in the nginx.conf.j2
template file, we can do the following:
#
nginx.conf.j2
server
{
listen
{{ web_port }};
# ...
The curly braces {{ }} indicate that this is a
variable that should be replaced with its value at runtime. When the template
is rendered, the {{ web_port }} expression will be replaced with the
value of the web_port variable.
You can also define variables in external files, which can
be useful for separating sensitive information like passwords and API keys from
your playbook code. Ansible supports several file formats for variables,
including YAML, JSON, and INI. For example, you can define variables in a YAML
file like this:
---
#
vars.yml
web_port: 80
app_version: 1.0.0
You can then include this file in your playbook using the vars_files
parameter:
----------------------------------------
---
- name: Deploy web server
hosts: web_servers
vars_files:
- vars.yml
tasks:
# ...
Using Variables in Tasks
You can define variables within a playbook and use them
within tasks. Here's an example playbook that uses variables to install
different packages on different hosts:
- name: Install Packages
hosts:
- web
- db
vars:
web_packages:
- httpd
- mod_ssl
db_packages:
- mysql-server
tasks:
- name: Install Packages on Web
Hosts
yum:
name: '{{ web_packages }}'
state: present
when: '''web'' in group_names'
- name: Install Packages on DB
Hosts
yum:
name: '{{ db_packages }}'
state: present
when: '''db'' in group_names'
In this playbook, we define two sets of packages for the web
and db hosts using variables (web_packages and db_packages). We then use the `when`
keyword to ensure that the tasks are only executed on the appropriate hosts.
Using Variables in Templates
You can use variables in templates to create dynamic
configuration files. Here's an example playbook that uses a template to create
an Apache virtual host configuration file:
------------------------------------
- name: Configure Apache Virtual
Host
hosts: web
vars:
server_name: example.com
docroot: /var/www/html/example
tasks:
- name: Create Apache Virtual
Host Configuration File
template:
src: templates/vhost.conf.j2
dest: /etc/httpd/conf.d/{{ server_name }}.conf
notify:
- restart httpd
In this playbook, we define two variables (server_name and
docroot) that are used in the template file (vhost.conf.j2) to create the
virtual host configuration file. We then use the notify keyword to restart the
Apache service when the configuration file is updated.
Using Variables in Roles
Roles are reusable Ansible components that can be used
across different playbooks. You can use variables within roles to create
flexible and customizable components. Here's an example role that installs and
configures the Nginx web server:
---
- name: Install Nginx
hosts: all
become: true
vars:
nginx_version: 1.20.1
tasks:
- name: Install Nginx
yum:
name: nginx-{{ nginx_version }}
state: present
- name: Configure Nginx
template:
src: templates/nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify:
- restart nginx
In this role, we define a variable (nginx_version) that is
used to install a specific version of Nginx. We also use a template file
(nginx.conf.j2) to create the Nginx configuration file. Finally, we use the
notify keyword to restart the Nginx service when the configuration file is
updated.
You can also use variables in other parts of your tasks,
such as command arguments, file paths, and conditional expressions. For
example:
- name: Create user account
user:
name: "{{ user_name
}}"
password: "{{ hashed_password
}}"
groups: "{{ user_groups
}}"
shell: /bin/bash
home: "/home/{{ user_name
}}"
state: present
become: yes
- name: Copy config file
copy:
src: "{{ app_config_file
}}"
dest: "/etc/myapp/config.yml"
mode: 0644
become: yes
- name: Stop service if it's
running
service:
name: myservice
state: stopped
when: myservice_state ==
"running"
Overriding Variables
Sometimes you may need to override a variable value for a
specific task or host. Ansible provides several ways to do this, including
using the --extra-vars command-line option, setting environment
variables, or passing variables through inventory files.
For example, you can override the web_port variable
for a specific host like this:
---
- name: Deploy web server
hosts: web_servers
vars:
web_port: 80
tasks:
- name: Install nginx
apt:
name: nginx
state: present
become: yes
- name: Configure nginx
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
mode: 0644
become: yes
vars:
web_port: 8080
Conclusion
Variables are a powerful feature of Ansible that allow you
to create flexible and customizable playbooks. In this bloga, we provided some
examples of how to use variables in tasks, templates, and roles. These examples
demonstrate the flexibility and power of Ansible, and how it can be used to
automate a wide range of tasks and scenarios.
Comments
Post a Comment